/*
 * Copyright (c) 2014-2015 Luca Agostini, Alessandro Cantini, Alessandro Dignani
 *
 * This file is part of MeteoCal.
 *
 * MeteoCal is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package it.polimi.se2.agostini_cantini_dignani.meteocal.model;

/**
 * @Author Alessandro Cantini
 */
public enum TemperatureUnit {
	KELVIN(" K", "Kelvin", 0.0f, 1.0f),
	CELSIUS(" °C", "Celsius", 273.15f, 1.0f),
	FAHRENHEIT(" °F", "Fahrenheit", 459.67f, 5.0f / 9.0f);

	private final String symbol;
	private final String name;
	private final Float sum;
	private final Float mult;

	TemperatureUnit(String symbol, String name, Float sum, Float mult) {
		this.symbol = symbol;
		this.name = name;
		this.sum = sum;
		this.mult = mult;
	}

	/**
	 * Converts a certain quantity of units from a unit to another
	 *
	 * @param quantity the temperature to convert
	 * @param from     the unit of the quantity to convert
	 * @param to       the unit of the converted quantity
	 * @return the converted quantity
	 */
	public static Float convert(Float quantity, TemperatureUnit from, TemperatureUnit to) {
		double kelvin = (quantity + from.sum) * from.mult;
		if (from == KELVIN && quantity < 0.0) {
			throw new IllegalArgumentException();
		}
		return (float) (kelvin / to.mult - to.sum);
	}

	public String getSymbol() {
		return symbol;
	}

	public String getName() {
		return name;
	}
}
